<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="author" content="" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" />
<link href="../../css/themes/default.css" rel="stylesheet" type="text/css"/>
<link href="../../css/themes/mac_os_x.css" rel="stylesheet" type="text/css"/>
<link href="../../css/themes/alphacube.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../js/prototype.js"></script>
<script type="text/javascript" src="../../js/scriptaculous.js"></script>
<script type="text/javascript" src="../../js/page.js"></script>
<script type="text/javascript" src="../../js/window.js"></script>
<script type="text/javascript" src="../../js/window_effects.js"></script>
<script type="text/javascript">
var win;
var usr_win;
var _win;
var id_alunni = new Array;
var messages = new Array('', 'Genitore inserito con successo', 'Genitore cancellato con successo', 'Genitore modificato con successo');

function user(id){
    //var newwin = window.open_centered("dettaglio_genitore.php?id="+id, "gen", 650, 450, "");
    win = new Window({className: "mac_os_x", url: "dettaglio_genitore.php?id="+id, top: 100, left: 30,  width:650, height:450, zIndex: 100, resizable: true, title: "Dettaglio genitore", showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:true, wiredDrag: true});
	if(id != 0)
		win.showCenter(false);
	else
    	win.show(false);

    _win = new Window({className: "mac_os_x", url: "elenco_alunni.php", top: 100, left: 700, width:350, height:450, zIndex: 100, resizable: true, title: "Elenco alunni", showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:true, wiredDrag: true});
}

function get_classes(){
    //alert("ok");
	var url = "<?php print $_SESSION['__config__']['root_site'] ?>admin/adm_parents/aj_get_classes.php";
	var req = new Ajax.Request(url,
			  {
			    	method:'post',
			    	parameters: {v: 1},
			    	onSuccess: function(transport){
			      		var response = transport.responseText || "no response text";
			      		<?php
		      				if(isset($_REQUEST['classe']))
		      					print("var c_cls = '".$_REQUEST['classe'].$_REQUEST['sezione']."';\n");
		      				else
		      					print("var c_cls = 0;\n");
		      			?>
			      		if(response == "ko"){
			      			alert("Impossibile completare l'operazione richiesta. Riprovare tra qualche secondo o segnalare l'errore al webmaster");
				     		return;
			     		}
			     		else{
			     			var dati = response.split(";");
			            	var x = document.getElementById("class_td");
			            	//x.innerHTML = "";
			            	document.getElementById("tdlink").style.display = "none";
			            	var sel = document.createElement("SELECT");
			            	sel.name = "mysel";
			            	sel.style.border = "1px solid #eee";
			            	sel.style.fontSize = "10px";
			            	sel.style.color = "#777";
			            	sel.style.width = "180px";
			            	sel.onchange = filtro;
			            	//alert(sel);
			            	//sel.name = "classe";
			            	f_opt = document.createElement("OPTION");
			            	f_opt.text = "Tutte";
			            	f_opt.value = 0;
			            	sel.add(f_opt, null);
			            	sel.selectedIndex = 0;
			            	idx = 0;
			            	for(i = 0; i < dati.length; i++){
			            		opt = document.createElement("OPTION");
			            		couple = dati[i].split("#");
			            		opt.value = couple[1];
			            		opt.text = couple[1];
			            		sel.add(opt, null);
			            		if(c_cls == couple[1]){
			            			idx = i;
			            			sel.selectedIndex = i+1;
			            		}
			            	}
			            	x.appendChild(sel);
			     		}
			    	},
			    	onFailure: function(){ alert("Si e' verificato un errore...") }
			  });
}

function filtro(){
	cls = document.forms[0].mysel.value.charAt(0);
	sez = document.forms[0].mysel.value.charAt(1);
	if(document.forms[0].mysel.value == 0)
		document.location.href = "genitori.php";
	else
		document.location.href = "genitori.php?classe="+cls+"&sezione="+sez;
}

function go(val){
	if(val == 1){
		if(trim(document.forms[0].parent.value) == "")
			document.location.href = "genitori.php";
		else{
			document.location.href = "genitori.php?nome="+trim(document.forms[0].parent.value);
		}
	}
	else{
		if(trim(document.forms[0].student.value) == "")
			document.location.href = "genitori.php";
		else{
			document.location.href = "genitori.php?aname="+trim(document.forms[0].student.value);
		}
	}
}

function filtro_nome(val){
	if(val == 2){
		x = document.getElementById("stud_td");
		x.innerHTML = "";
		x.innerHTML = "<input type='text' name='student' style='font-size: 10px; width: 150px; border: 1px solid #eee; color: #777' />&nbsp;&nbsp;<input type='button' value='filtra' style='border: 1px solid ' onclick='go(2)' />";
	}
	else{
		x = document.getElementById("par_td");
		x.innerHTML = "";
		x.innerHTML = "<input type='text' name='parent' style='font-size: 10px; width: 150px; border: 1px solid #eee; color: #777' />&nbsp;&nbsp;<input type='button' value='filtra' style='border: 1px solid ' onclick='go(1)' />";
	}
}

</script>
<title>Registro elettronico</title>
</head>
<body <?php if(isset($_REQUEST['msg'])){ ?>onload="openInfoDialog(messages[<?php print $_REQUEST['msg'] ?>], 2)"<?php } ?>>
	<div id="header">
		<div class="wrap" style="text-align: center">
			<?php include "../header.php" ?>
		</div>
	</div>
	<div class="wrap">
	<div id="main" style="background-color: #FFFFFF; padding-bottom: 30px; width: 100%">
    <form>
        <table class="admin_table">
            <tr class="admin_title_row">
                <td style="font-weight: bold" colspan="3" align="center">Elenco genitori</td>
            </tr>
            <tr>
                <td class="adm_titolo_elenco" style="width: 30%" id="par_td"><a id="tdlink_std" href="#" onclick="filtro_nome(1)" style="text-decoration: none">Nome e cognome</a></td>
                <td class="adm_titolo_elenco" style="width: 50%" id="stud_td"><a id="tdlink_std" href="#" onclick="filtro_nome(2)" style="text-decoration: none">Alunni</a></td>
                <td class="adm_titolo_elenco _center" style="width: 20%" id="class_td"><a id="tdlink" href="#" onclick="get_classes()" style="text-decoration: none">Classi</a></td>
            </tr>
            <tr class="admin_row">
                <td id="row_before" colspan="3"></td>
            </tr>
            <?php
            $x = 0;
            if($res_user->num_rows > $limit)
                $max = $limit;
            else
                $max = $res_user->num_rows;

            $id_genitore = 0;
            $classe = "";
            $final_user = "";
            $final_uid = 0;
            $figli = array();
            $classi = array();
            while($user = $res_user->fetch_assoc()){
                if($x > $limit) break;
                
                if($id_genitore != $user['id_genitore'] && $id_genitore != 0){
                	$x++;
            ?>
            <tr class="admin_row<?php if($x % 2) print(" odd") ?>">
                <td><a href="#" onclick="user(<?php print $final_uid ?>)"><?php print $final_user ?></a></td>
                <td><?php print join(", ", $figli); ?></td>
                <td><?php print join(", ", $classi); ?></td>
            </tr>
            <?php           		
            		$figli = array_slice($figli, 0, 0);
                	$classi = array_slice($classi, 0, 0);
				}

				if(!in_array($user['al_lname']." ".$user['al_name'], $figli))
					array_push($figli, $user['al_lname']." ".$user['al_name']);
				if(!in_array($user['classe'], $classi))
					array_push($classi, $user['classe']);
				$id_genitore = $user['id_genitore'];
                
                $final_user = $user['cognome']." ".$user['nome'];
                $final_uid = $user['id_genitore'];
            }
            //print(count($figli)."-".count($classi));
            $x++;
            ?>
            <tr class="admin_row<?php if($x % 2) print(" odd") ?>">
                <td><a href="#" onclick="user(<?php print $final_uid ?>)"><?php print $final_user ?></a></td>
                <td><?php print join(", ", $figli); ?></td>
                <td class="_center"><?php print join(", ", $classi); ?></td>
            </tr>
            <?php
            include "../../shared/navigate.php";
            ?>
            <tr class="admin_row admin_menu">
                <td colspan="3" >
                	<a href="#" onclick="user('0')" style="margin-right: 10px">Nuovo genitore</a>|
                    <a href="../index.php" style="margin-left: 10px">Torna al menu</a>
                </td>
            </tr>
            <tr class="admin_row">
                <td colspan="3"></td>
            </tr>
        </table>
        </form>
        </div>
        <div id="footer">
			<p>Design: Luka Cvrk - <a href="http://www.solucija.com" title="Free Web Templates">Solucija</a></p>
		</div>
    </div>				
</body>
</html>